﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace HomeBuyerTools.Views.GainOnSaleOfHome
{
    public partial class Result : PhoneApplicationPage
    {
        public Result()
        {
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {

            string sp = "";
            string Basis = "";
            string fu = "";
            string cr = "";
            string pp = "";
            string te = "";
            string ce = "";
            string tb = "";

            NavigationContext.QueryString.TryGetValue("sp", out sp);
            NavigationContext.QueryString.TryGetValue("Basis", out Basis);
            NavigationContext.QueryString.TryGetValue("fu", out fu);
            NavigationContext.QueryString.TryGetValue("cr", out cr);
            NavigationContext.QueryString.TryGetValue("pp", out pp);
            NavigationContext.QueryString.TryGetValue("te", out te);
            NavigationContext.QueryString.TryGetValue("ce", out ce);
            NavigationContext.QueryString.TryGetValue("tb", out tb);
            double saleprice = Convert.ToDouble(sp);
            double Bas = Convert.ToDouble(Basis);
            double Fixup = Convert.ToDouble(fu);
            double commissionrate = Convert.ToDouble(cr);
            double purchaseprice = Convert.ToDouble(pp);
            double taxexclusion = Convert.ToDouble(te);
            //double claimexclusion = Convert.ToDouble(ce);
            //double taxbracket = Convert.ToDouble(tb);

            if (sp != "")
            {
                tblkAmountRealizedOnSale.Text = Convert.ToString(saleprice - (commissionrate / 100 * saleprice));
            }
            else
            {
                tblkAmountRealizedOnSale.Text = "0";

            }
            if (sp != "")
            {
                tblkAdjustedSalePrice.Text = Convert.ToString(Convert.ToDouble(tblkAmountRealizedOnSale.Text) - Fixup);
            }
            else
            {
                tblkAdjustedSalePrice.Text = "0";

            }
            if (sp != "")
            {
                tblkGainRealizedOnSale.Text = Convert.ToString(Convert.ToDouble(tblkAmountRealizedOnSale.Text) - Bas);
            }
            else
            {
                tblkGainRealizedOnSale.Text = "0";

            }

            if (purchaseprice >= Convert.ToDouble(tblkAdjustedSalePrice.Text))
            {

                tblkGainTaxableThisYear.Text = "0";

            }
            else
            {
                if (pp == "0")
                {
                    tblkGainTaxableThisYear.Text = tblkGainRealizedOnSale.Text;
                }
                else
                {
                    if (pp != "0")
                    {
                        Math.Min(Convert.ToDouble(tblkAdjustedSalePrice.Text) - purchaseprice, Convert.ToDouble(tblkGainRealizedOnSale.Text));
                    }
                    else
                    {
                        tblkGainTaxableThisYear.Text = "0";
                        
                    }
                }
            }

            if (Convert.ToDouble(tblkGainTaxableThisYear.Text) >= Convert.ToDouble(tblkGainRealizedOnSale.Text))
            {
                tblkGainOfWhichTaxIsDeferred.Text = "0";
                
            }
            else
            {

                double GainOfWhichTaxIsDeferred = Convert.ToDouble(tblkGainRealizedOnSale.Text.Trim()) - Convert.ToDouble(tblkGainTaxableThisYear.Text.Trim());
                tblkGainOfWhichTaxIsDeferred.Text = Convert.ToString(GainOfWhichTaxIsDeferred);                
            }

            double CapitalGainsTax = saleprice + Bas + Fixup + commissionrate + purchaseprice + taxexclusion;
            if (CapitalGainsTax == 0)
            {
                tbCapitalGainsTax.Text = "0";

            }
            else
            {
                

            }

            
        }
    }
}
